package com.jwcao.common.cast.operation.instance;

import com.jwcao.common.cast.operation.access.AccessOperation;
import com.jwcao.common.scan.object.ScanClass;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;

/**
 * name: ArgInstanceOperation description: TODO
 *
 * @author caojingwei date: 2023/3/23 17:14
 */
@Slf4j
public class ArgInstanceOperation implements InstanceOperation {

    private final ScanClass targetClass;

    private final List<AccessOperation> parameterList;

    public ArgInstanceOperation(ScanClass targetClass,
        List<AccessOperation> parameterList) {
        this.targetClass = targetClass;
        this.parameterList = parameterList;
    }

    @Override
    public Object execute(Object sourceObject) {
        return targetClass.instance(parameterList.stream().map(a -> a.execute(sourceObject)).toArray());
    }

    public List<AccessOperation> excludeInjectionOperation(List<AccessOperation> accessOperationList) {
        // 这里由于都是一套对象，直接比较指针就可以了。
        return accessOperationList.stream().filter(o -> parameterList.contains(o)).collect(Collectors.toList());
    }
}
